using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using FreeLancer.HRB.Domains;
using FreeLancer.HRB.Services;
using FreeLancer.HRB.Core;

namespace FreeLancer.HRB.WebApp
{
  public partial class Site : System.Web.UI.MasterPage
  {
    private const string HeroesCategoryId = "6d1876ac-de48-4570-aa99-6479fd4b2fec";
    private const string KnowledgeCategoryId = "6f20049e-3c2e-4068-96b0-5cd15e67f301";

    protected int _languageId;

    protected void Page_Init(object sender, EventArgs e)
    {
      if (Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName == "vi")
        _languageId = 1;
      else
        _languageId = 2;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      DataSourceHeroes.SelectParameters.Clear();
      DataSourceHeroes.SelectParameters.Add("LanguageId", _languageId.ToString());
      DataSourceHeroes.SelectParameters.Add("CategoryId", HeroesCategoryId);
      DataSourceHeroes.SelectParameters.Add("IsPublished", "True");
      DataSourceHeroes.SelectParameters.Add("HeaderImageFolder",
        Utils.RelativeWebRoot + "Images/PostImages/");

      DataSourceKnowledge.SelectParameters.Clear();
      DataSourceKnowledge.SelectParameters.Add("LanguageId", _languageId.ToString());
      DataSourceKnowledge.SelectParameters.Add("CategoryId", KnowledgeCategoryId);
      DataSourceKnowledge.SelectParameters.Add("IsPublished", "True");
      DataSourceKnowledge.SelectParameters.Add("HeaderImageFolder",
        Utils.RelativeWebRoot + "Images/PostImages/");

      LoadAds();

      ASPxNewsControlKnowledge.NavigateUrlFormatString = Utils.RelativeWebRoot + "Post.aspx?id={0}";
      ASPxNewsControlHeroes.NavigateUrlFormatString = Utils.RelativeWebRoot + "Post.aspx?id={0}";

      anchorLogin.HRef = Utils.RelativeWebRoot + "Login.aspx";
    }

    private void LoadAds()
    {
      int total = 0;
      AdvsService service = new AdvsService();
      TList<Advs> ads = service.GetPaged(string.Empty, "[Index] ASC", 0, 5, out total);

      anchorAdBottom01.Visible = false;
      anchorAdBottom02.Visible = false;
      anchorAdBottom03.Visible = false;
      anchorAdBottom04.Visible = false;
      anchorAdBottom05.Visible = false;

      if (ads.Count > 0)
      {
        anchorAdBottom01.HRef = ads[0].LinkPage;
        imageAdBottom01.Src = Utils.RelativeWebRoot + "Images/AdImages/" + ads[0].Path;
        imageAdBottom01.Alt = ads[0].Tooltip;
        anchorAdBottom01.Visible = true;
      }
      if (ads.Count > 1)
      {
        anchorAdBottom02.HRef = ads[1].LinkPage;
        imageAdBottom02.Src = Utils.RelativeWebRoot + "Images/AdImages/" + ads[1].Path;
        imageAdBottom02.Alt = ads[1].Tooltip;
        anchorAdBottom02.Visible = true;
      }
      if (ads.Count > 2)
      {
        anchorAdBottom03.HRef = ads[2].LinkPage;
        imageAdBottom03.Src = Utils.RelativeWebRoot + "Images/AdImages/" + ads[2].Path;
        imageAdBottom03.Alt = ads[2].Tooltip;
        anchorAdBottom03.Visible = true;
      }
      if (ads.Count > 3)
      {
        anchorAdBottom04.HRef = ads[3].LinkPage;
        imageAdBottom04.Src = Utils.RelativeWebRoot + "Images/AdImages/" + ads[3].Path;
        imageAdBottom04.Alt = ads[3].Tooltip;
        anchorAdBottom04.Visible = true;
      }
      if (ads.Count > 4)
      {
        anchorAdBottom05.HRef = ads[4].LinkPage;
        imageAdBottom05.Src = Utils.RelativeWebRoot + "Images/AdImages/" + ads[4].Path;
        imageAdBottom05.Alt = ads[4].Tooltip;
        anchorAdBottom05.Visible = true;
      }
    }
  }
}
